﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MIPS_Simulator
{
    class Memory
    {
        private const long memorySize = 4294967296;
        private Dictionary<long, long> dataMemory;
        private bool memRead;
        public bool MemRead
        {
            set
            {
                memRead = value;
            }
            get
            {
                return memRead;
            }
        }

        private bool memWrite;
        public bool MemWrite
        {
            set
            {
                memWrite = value;
            }
            get
            {
                return memWrite;
            }
        }
        public Memory()
        {
            dataMemory = new Dictionary<long, long>();
        }

        public void storeWord(long data, long location)
        {
            if (memWrite == false)
                throw new WriteNotEnabledException();

            if (location > memorySize || location < 0)
                throw new IndexOutOfRangeException("Memory index of of range");
            try
            {
                dataMemory.Add(location, data);
            }
            catch (ArgumentException e)
            {
                dataMemory[location] = data;
            }
                
        }

        public long loadWord(long location)
        {
            long value;
            if (memRead == false)
                throw new ReadNotEnabledException();

            if (location > memorySize || location < 0 )
                throw new IndexOutOfRangeException("Memory index of of range");

            dataMemory.TryGetValue(location, out value);
            return value;

        }
    }
}
